package com.atguigu.netty.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.UnpooledByteBufAllocator;

/**
 * @author zhangcheng
 * @date 2024/3/20 22:55
 * @description
 */
public class ByteBufDemo {

    public static void main(String[] args) {
        UnpooledByteBufAllocator unpooledByteBufAllocator = new UnpooledByteBufAllocator(true);

        ByteBuf buffer = unpooledByteBufAllocator.buffer();

        String content = "hello netty";
        buffer.writeBytes(content.getBytes());

        System.out.println("after netty readableBytes = " + buffer.readableBytes());
        System.out.println("after netty readerIndex = " + buffer.readerIndex());
        System.out.println("after netty writerIndex = " + buffer.writerIndex());

        buffer.writeBytes("666".getBytes());

        System.out.println("after 666 readableBytes = " + buffer.readableBytes());
        System.out.println("after 666 readerIndex = " + buffer.readerIndex());
        System.out.println("after 666 writerIndex = " + buffer.writerIndex());

        buffer.markReaderIndex();


        while (buffer.isReadable()){
            System.out.println(Character.toChars(buffer.readByte()));
        }

        System.out.println("after readByte readableBytes = " + buffer.readableBytes());
        System.out.println("after readByte readerIndex = " + buffer.readerIndex());
        System.out.println("after readByte writerIndex = " + buffer.writerIndex());


        buffer.resetReaderIndex();

        System.out.println("after resetReaderIndex readableBytes = " + buffer.readableBytes());
        System.out.println("after resetReaderIndex readerIndex = " + buffer.readerIndex());
        System.out.println("after resetReaderIndex writerIndex = " + buffer.writerIndex());

        buffer.clear();

        System.out.println("after clear readableBytes = " + buffer.readableBytes());
        System.out.println("after clear readerIndex = " + buffer.readerIndex());
        System.out.println("after clear writerIndex = " + buffer.writerIndex());


    }
}
